<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品类别信息管理</title>
	</head>
	<body>
		<center>
			<h3>商品类别信息管理</h3>
			<?php 
				//1.导入配置文件
				require("../../public/config.php");
				$link = @mysqli_connect(HOST,USER,PASS,DBNAME);
				//判断连接数据库是否成功
				if(!$link){
					die("数据库连接失败！原因是：".mysqli_connect_error());
				}
				
				mysqli_set_charset($link,"utf8");//设置编码格式
				//根据a的值来进行相应的操作
				switch($_GET['a']){
					case 'insert':
						$pid = $_POST['pid'];
						$path = $_POST['path'];
						$name = $_POST['name'];
						$sql = "insert into type values(null,'{$name}','{$pid}','{$path}')";//设置sql语句
						
						mysqli_query($link,$sql);//发送sql语句
						// echo mysqli_insert_id($link);
						//判断是否添加成功
						if(mysqli_insert_id($link)>0){
							echo '<script>alert("添加成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}else{
							echo "添加失败";
							header("refresh:2;url=index.php");
							exit();
						}
						break;
					//根据a传来的值进行删除操作
					case "del":
						$id = $_GET['id'];//获取传过来的id值
						
						$sql = "select * from type where path like \"%$id%\"";   //设置sql语句进行模糊查询
						$result = mysqli_query($link,$sql);		//发送sql语句并接收结果集
						$rows = mysqli_fetch_assoc($result);	//解析结果集
						
						if(!empty($rows)){					//判断所删除的有没有子目录
							echo "有子目录，不可以删除！";
							
							header("refresh:2;url=index.php");	//跳转
							
						}else{
							$sql = "delete from  type where id=$id";	//如果没有子目录则进行删除操作
							mysqli_query($link,$sql);		//发送sql语句并接收结果集
							if(mysqli_affected_rows($link)){ 	
								echo "删除成功！";
								// header("refresh:2;url=index.php");
								header("Location:".$_SERVER["HTTP_REFERER"]);
							}else{ 
								echo '<script>alert("删除失败！"); </script>';
								header("refresh:0;url=index.php");
								exit();
							}
							break;
						}
						
						//根据a传过来的值对数据进行更改
					case "update":				
						$id = $_POST['id'];
						// echo $id;
						$pid = $_POST['pid'];
						$path = $_POST['path'];
						$name = $_POST['name'];
						
						$sql =  "update type set name='{$name}' where id='{$id}'";//设置所要更新的sql语句
						
						mysqli_query($link,$sql);//发送sql语句并接收
						
						if(mysqli_affected_rows($link)){		//判断是否修改成功
							echo '<script>alert("修改成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}else{
							echo "修改失败！".mysqli_error($link);
							header("refresh:2;url=index.php");
							exit();
						}
						mysqli_close($link);
						
						break;
					
				}
			 ?>
		</center>
	</body>
</html>